<template>

    <!-- 查询条件 -->
    <div style="border: 1px solid black; margin: 10px; padding: 10px;">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="名称">
                <el-input v-model="formInline.LineName" clearable />
            </el-form-item>

            <el-form-item label="编码">
                <el-input v-model="formInline.LineCode" clearable />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="LienpShow">查询</el-button>
                <el-button type="warning" @click="reset">重置</el-button>
                <el-button type="success" @click="LienpkAdd">添加</el-button>
            </el-form-item>
        </el-form>
    </div>

    <!-- 显示 -->
    <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="index" label="序号" width="50" />

        <el-table-column prop="lineCode" label="编码" width="120" />
        <el-table-column prop="lineName" label="名称" width="120" />
        <el-table-column prop="wsName" label="所属车间" width="120" />
        <el-table-column prop="lineMsg" label="描述" width="120" />
        <el-table-column prop="addProp" label="创建人" width="120" />
        <el-table-column prop="addTime" label="添加时间" width="200">
            <template #default="script">
                {{ moment(script.row.addTime).format("YYYY-MM-DD HH:mm:ss") }}
            </template>
        </el-table-column>
        <el-table-column prop="updTime" label="更新时间" width="200">
            <template #default="scope">
                {{ scope.row.updTime == null ? "" : moment(scope.row.updTime).format("YYYY-MM-DD HH:mm:ss") }}
            </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" min-width="120">
            <template #default="script">
                <el-button link type="primary" size="small" @click="DelLienp(script.row.id)">
                    删除
                </el-button>
                <el-button link type="primary" size="small" @click="UpLienp(script.row)">
                    修改
                </el-button>
            </template>
        </el-table-column>

    </el-table>


    <!-- 添加 -->
    <el-dialog v-model="LienpAddVis" title="添加产线" width="500">
        <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">

            <el-form-item label="名称" prop="lineName">
                <el-input v-model="ruleForm.lineName" />
            </el-form-item>

            <el-form-item label="描述" prop="lineMsg">
                <el-input v-model="ruleForm.lineMsg" :rows="2" type="textarea" />
            </el-form-item>

            <el-form-item label="所属车间" prop="wsId">
                <el-select v-model="ruleForm.wsId" placeholder="选择车间">
                    <el-option v-for="item in LienGet" :Key="item.id" :label="item.wsCode + '/' + item.wsName"
                        :value="item.id" />
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    添加
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog v-model="UpLienpTableVis" title="修改产线" width="800">
        <el-form :model="UpLienData">

            <el-form-item label="名称" label-width="120px">
                <el-input v-model="UpLienData.lineName" autocomplete="off" />
            </el-form-item>

            <el-form-item label="描述" prop="lineMsg">
                <el-input v-model="UpLienData.lineMsg" :rows="2" type="textarea" />
            </el-form-item>

            <el-form-item label="所属车间" label-width="120px">
                <el-select v-model="UpLienData.wsId" placeholder="选择车间">
                    <el-option v-for="item in LienGet" :Key="item.id" :label="item.wsCode + '/' + item.wsName"
                        :value="item.id" />
                </el-select>
            </el-form-item>

        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="LienpManageUp">
                    修改
                </el-button>
            </div>
        </template>
    </el-dialog>


</template>

<script lang="ts" setup>
import Maxios from '@/Http/MesHelp'
import { ref, reactive, onMounted } from 'vue'
import moment from 'moment'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'


const Page = reactive({
    PageIndex: 1,
    PageSize: 10
})
const formInline = reactive({
    LineCode: '',
    LineName: ''
})

//显示 查询 分页
const tableData = ref([])

const reset = () => {
    formInline.LineCode = "";
    formInline.LineName = "";
    LienpShow()
}

const LienpShow = () => {
    Maxios.get("/api/factory/LienpManageShow", {
        params:
        {
            PageIndex: Page.PageIndex, PageSize: Page.PageSize,
            LineName: formInline.LineName, LiLineCode: formInline.LineCode
        }
    }).then(res => {
        console.log(res.data.data.list)
        tableData.value = res.data.data.list
    })
}

//删除
const DelLienp = (id: any) => {
    ElMessageBox.confirm(
        '你确定要删除吗?',
        '删除警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            // ElMessage({
            //     type: 'success',
            //     message: 'Delete completed',
            // })

            Maxios.post("/api/factory/LienpManageDel", { id: id }).then(res => {
                if (res.data.code == 200) {
                    ElMessage.success("删除成功");
                    LienpShow()
                }
                else {
                    ElMessage.success("删除失败");
                    LienpShow()
                }
            })

        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '您取消了删除',
            })
        })
}

//显示车间
const LienGet = ref([{
    wsCode:"",
    wsName:"",
    id:""
}])
const LienpGet = () => {
    Maxios.get("/api/factory/LienpManageGet").then(res => {
        LienGet.value = res.data.data
        console.log(LienGet.value);
    })
}

//添加
const LienpAddVis = ref(false)
const LienpkAdd = () => {
    LienpAddVis.value = true
}
interface RuleForm {
    lineCode: string
    lineName: string
    lineMsg: string
    wsId: any
    addProp: string
}
const User = reactive({
    Name: localStorage.getItem("userNickname") || ''
})
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
    lineCode: '',
    lineName: '',
    lineMsg: '',
    wsId: '',
    addProp: User.Name
})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules<RuleForm>>({
    lineName: [
        { required: true, message: '名称不能为空', trigger: 'blur' },
    ],
    lineMsg: [
        { required: true, message: '描述不能为空', trigger: 'blur' },
    ],
    wsId: [
        { required: true, message: '请选择所属车间', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log('submit!')

            Maxios.post("/api/factory/LinepManageAdd", ruleForm).then(res => {
                if (res.data.code == 200) {
                    ElMessage.success("添加成功")
                    LienpAddVis.value = false
                    LienpShow()
                }
                else {
                    ElMessage.error("添加失败")
                    LienpAddVis.value = false
                    LienpShow()
                }
            })

        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
    value: `${idx + 1}`,
    label: `${idx + 1}`,
}))


//修改
const UpLienpTableVis = ref(false)
const UpLienData = reactive({
    id: '',
    lineName: '',
    lineMsg: '',
    wsId: ''
})
const UpLienp = (data: any) => {
    UpLienpTableVis.value = true
    UpLienData.id = data.id
    UpLienData.lineMsg = data.lineMsg
    UpLienData.lineName = data.lineName
    UpLienData.wsId = data.wsId
}
const LienpManageUp = () => {
    Maxios.post("/api/factory/LienpManageUp", UpLienData).then(res => {
        if (res.data.code == 200) {
            ElMessage.success("修改成功")
            UpLienpTableVis.value = false
            LienpShow()
        }
        else {
            ElMessage.success("修改失败")
            UpLienpTableVis.value = false
            LienpShow()
        }
    })
}


onMounted(() => {
    LienpShow()
    LienpGet()
})

</script>
